<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class HT_Model_administrator_models_comments extends Zend_Db_Table {//ten class fai viet hoa
    protected $_db;
    public function __construct() {
    	$this->_name = "object_comments";
    	$this->_db = Zend_Registry::get('dbMain');
    	parent::init();
    }
    
    
    public function recentDate($time, $configLang = 'vn'){
    	if($configLang == "en") {
    		$langHe['year'] = "year";
    		$langHe['month'] = "month";
    		$langHe['day'] = "day";
    		$langHe['hour'] = "hour";
    		$langHe['minute'] = "minute";
    		$langHe['second'] = "second";
    		$langHe['s ago'] = "s ago";
    	}else{
    		$langHe['year'] = "năm";
    		$langHe['month'] = "tháng";
    		$langHe['day'] = "ngày";
    		$langHe['hour'] = "giờ";
    		$langHe['minute'] = "phút";
    		$langHe['second'] = "giây";
    		$langHe['s ago'] = "cách đây";
    	}
    	$from = null;
    	if ($from == null) {
    		$from = time();
    	}
    	$time = $from - $time;
    
    	$chunks = array(
    			array(60 * 60 * 24 * 365 , $langHe['year']),
    			array(60 * 60 * 24 * 30 , $langHe['month']),
    			array(60 * 60 * 24 , $langHe['day']),
    			array(60 * 60 , $langHe['hour']),
    			array(60 , $langHe['minute']),
    			array(1 , $langHe['second'])
    	);
    
    	for ($i = 0, $j = count($chunks); $i < $j; $i++) {
    		$seconds = $chunks[$i][0];
    		$name = $chunks[$i][1];
    		if (($count = floor($time / $seconds)) != 0) {
    			break;
    		}
    	}
    	if($configLang == "en") {
    		$print = ($count == 1 || $count == 0) ? '1 '.$name : "$count {$name}".$langHe['s ago'];
    	}else {
    		$print = ($count == 1 || $count == 0) ? '1 '.$name : $langHe['s ago']." $count {$name}";
    	}
    	return $print;
    
    }

    public function getComments($objectId,$type,$getActiveComment = 1) {
    	/*
    	 * $object is: news, post, video, ...., so that why we need $type
    	* */
    	if($getActiveComment) $sqlPlus = " AND cmm.status = 1 ";
    	$sql = "SELECT 
    				cmm.comment_id commentId, cmm.comment,cmm.comment_time commentTime,
    				us.avatar,CONCAT(us.firstname,' ',us.lastname) fullName,us.user_name userName, us.sex 
    			FROM object_comments cmm
    			INNER JOIN objects obj ON obj.object_id = cmm.object_id
    			INNER JOIN user us ON cmm.user_name = us.user_name
    			WHERE obj.object_id = ".(int)$objectId." AND obj.object_type_id = ".(int)$type." $sqlPlus ORDER BY cmm.comment_id DESC";
        try{
        $writer = new Zend_Log_Writer_Stream('/var/www/html/wish.vn/www/log_zend/log.txt');
        $logger = new Zend_Log($writer);
        $logger->info($sql);
        
        }  catch (Zend_Exception $e) {
            echo "Caught exception: " . get_class($e) . "\n";
            echo "Message: " . $e->getMessage() . "\n";
        }
        
    	$comments = $this->_db->fetchAll($sql,array($objectId,$type));
    	$this->getUserLikes($comments);
    	return $comments;
    }
    
    public function getUserLikes(&$comments) {
    	for($i=0;$i<sizeof($comments);$i++){
    		$commentId = $comments[$i]['commentId'];
    		$sql = "SELECT like_id likeId,user_name userName, time
    			FROM object_likes
    			WHERE comment_id = ? ORDER BY time DESC";
    		$likes = $this->_db->fetchAll($sql,array($commentId));
    		$comments[$i]['likes'] = $likes;
    		$comments[$i]['totalLike'] = sizeof($likes);
    	}
    }
    
    public function checkCommentOwner($commentId,$userName){
    	$sql = "SELECT user_name sysUsername FROM object_comments WHERE comment_id = ?";
    	$sysUsername = $this->_db->fetchOne($sql,array($commentId));
    	$sysUsername = trim($sysUsername);
    	$userName	= trim($userName);
    	if($sysUsername === $userName){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    public function convertNews(){
    	$this->deleteLinkNews();
    	$sql = "SELECT id_news newsId,title_vn title FROM news";
    	$listNews = $this->_db->fetchAll($sql);
    	foreach((array)$listNews as $news){
    		$this->insertLinkNews($news['newsId'],$news['title']);
    	}
    }
    
    private function insertLinkNews($newsId,$title){
    	$this->_name = "objects";
    	$data = array('object_id'=>$newsId,'object_type_id'=>2,'object_description'=>$title);
    	$this->insert($data);
    	$this->_name = "object_comments";
    }
    
    private function deleteLinkNews(){
    	$this->_name = "objects";
    	$this->delete("object_type_id = 2");
    	$this->_name = "object_comments";
    }
    
    public function addLike($userName,$commentId){
    	 $this->_name = "object_likes";
    	 $data = array();
    	 $data['user_name'] 	= $userName;
    	 $data['comment_id'] 	= $commentId;
    	 $data['time'] 			= time();
    	 $this->insert($data);
    	 $this->_name = "object_comments";
    }
    
    public function removeLike($likeId){
    	$this->_name = "object_likes";
    	$data = array();
    	$this->delete("like_id=".(int)$likeId);
    	$this->_name = "object_comments";
    }
    
}
?>
